using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Pavilion
{
    public class ImageLayer
    {
        private float speed;
        private string name;
        private DrawItem drawItem;
        private List<Frame> frameList;
        private Vector2 position;

        public ImageLayer(DrawVisibilityGroup drawVisibilityGroup)
        {
            position = new Vector2();
            frameList = new List<Frame>();
            drawItem = new DrawItem(0, DrawItem_Draw);
            DrawManager.Instance.Add(drawItem);
            drawVisibilityGroup.Add(drawItem);
        }

        public void Dispose()
        {
            drawItem.Dispose();
            drawItem = null;

            foreach (Frame frame in frameList)
                frame.Unload();
        }

        public void Deserialize(XElement xmlProperties)
        {
            name = xmlProperties.Attribute("Name").Value;
            speed = float.Parse(xmlProperties.Attribute("Speed").Value);
            drawItem.DrawOrder = double.Parse(xmlProperties.Attribute("DrawOrder").Value);
        }

        public void Load(string directoryPath)
        {
            string[] directoryList = Directory.GetDirectories(directoryPath + @"\" + name);

            for (int i = 0; i < directoryList.Length; i++)
            {
                Frame frame = new Frame();
                frame.CreateTexturesFromFolder(directoryList[i]);
                frameList.Add(frame);
            }
        }

        /// <summary>
        /// Updates the position of the current background image by the speed that is passed in.
        /// </summary>
        /// <param name="speed">Moves the background image. Left if positive. Right if negative.</param>
        public void Update(float xDistance)
        {
            if (frameList.Count == 0)
                return;

            position.X += xDistance * speed;

            if (frameList[0].Width + position.X < 0)
            {
                Frame frame = frameList[0];
                position.X += frame.Width;
                frameList.RemoveAt(0);
                frameList.Add(frame);
            }
            else if (position.X > 0)
            {
                Frame frame = frameList[frameList.Count - 1];
                position.X -= frame.Width;
                frameList.RemoveAt(frameList.Count - 1);
                frameList.Insert(0, frame);
            }
        }

        private void DrawItem_Draw()
        {
            if (frameList.Count == 0)
                return;

            int totalWidth = 0;
            int count = 0;

            while (totalWidth <= GameEngine.WindowWidth + (-position.X))
            {
                frameList[count].Draw(new Vector2(position.X + totalWidth, 0));

                totalWidth += frameList[count].Width;
                count++;
                
                if (count == frameList.Count)
                    count = 0;
            }
        }
    }
}
